{include file="../application/admin/view/header.html" /}
<link rel="stylesheet" type="text/css" href="/static/css/uploadify.css">
</head>
<body>
<div class="container">
	<div class="col-md-12">
		<div class="alert kesn_prompt"></div>
	</div>
	<div class="row">
		<form class="form-horizontal">
			 <div class="form-group">
			 	<div id="warehouse_id" style="display: none;">{$warehouse.id}</div>
			    <label for="warehouse_name" class="col-sm-2 control-label">仓库名</label>
			    <div class="col-sm-9">
			      <input type="text" class="form-control" id="warehouse_name" placeholder="" value="{$warehouse.warehouse}" disabled="disabled">
			    </div>
			 </div>
		</form>
	</div>

	<form class="navbar-form navbar-right" action="save_warehouse" role="search"  method="get">
	  <div class="form-group">
	  	<input type="number" name="warehouse_id" value="{$warehouse.id}" style="display: none;">
	    <div class="input-group input-group-rounded">
	      <input class="form-control" id="keyword" name="keyword" type="search" placeholder="请输入用户名">
	      <span class="input-group-btn">
	        <button type="submit" class="btn kesn_search"><span class="fui-search"></span></button>
	      </span>
	    </div>
	  </div>
	</form>

	 <div align="center">
        <button class="btn btn-primary btn-sm" id="add_user">单个用户添加<span class="fui-plus btn-tip"></span></button>
		<button class="btn btn-primary btn-sm" data-toggle="modal" data-target="#import_users">批量导入用户<span class="glyphicon glyphicon-log-in btn-tip"></span></button>
		<a class="btn btn-primary btn-sm" role="button" href="export_users?warehouse_id={$warehouse.id}&warehouse_name={$warehouse.warehouse}">批量导出用户<span class="glyphicon glyphicon-log-out btn-tip"></span></a>
    </div>



	
		 <div class="modal fade" id="import_users" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
		  <div class="modal-dialog modal-lg" role="document">
		    <div class="modal-content">
		       <div class="modal-header">
		        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
		        <h6 class="modal-title" id="gridSystemModalLabel">用户批量导入</h6>
		      </div>


		      <div class="modal-body">
				
		      <div id="fileQueue"></div>
				<input type="file" name="import_file" id="uploadify" multiple="true"/>
				<p>
				    <a href="javascript:$('#uploadify').uploadify('upload','*');">上传</a>| 
				    <a href="javascript:$('#uploadify').uploadify('cancel');">取消上传</a>
				</p>
		      </div>
		    </div>
		  </div>
		</div>


	<div class="row">
		<table class="table table-hover">
			<thead>
				<th>ID</th>
				<th>用户名</th>
				<th class="password_header" style="display: none;">密码</th>
				<th>创建时间</th>
				<th>操作</th>
			</thead>
			<tbody>
				{volist name="ouser_list" id="ouser"}
				<tr class="_item"> 
					<th scrope="row">{$ouser.id}</th>
					<td class="td-ouser_name">{$ouser.user_name}</td>
					<td class="password" style="display: none;"></td>
					<td>{$ouser.created}</td>
					<td><div>
						<a href="#" class="btn btn-primary edit_ouser btn-sm" data-status="0">编辑<span class="fui-new btn-tip"></span></a>
						<a href="#" class="btn btn-danger kesn_del btn-sm" data-_id="{$ouser.id}" data-_name="{$ouser.user_name}">删除<span class="fui-cross btn-tip"></span></a>
					</div></td>
				</tr>
				{/volist}
		</tbody>
	</table>
	{$page}
	</div>

	<!-- <div class="row">
		<button class="btn btn-info ouser_save">淇濆瓨</button>
 	</div> -->

</div>

<footer>
<script type="text/javascript" src="/static/js/vendor/jquery.min.js"></script><!-- 
	<script type="text/javascript" src="/static/js/bootstrap.js"></script> -->
<script type="text/javascript" src="/static/js/flat-ui.js"></script>
<script type="text/javascript" src="/static/js/admin.js"></script>
<script type="text/javascript" src="/static/js/jquery.uploadify.js"></script>
<script type="text/javascript">
	 $("#uploadify").uploadify({
    	'buttonText':'选择EXCLE导入文件',
    	'swf'     : '/static/swf/uploadify.swf',
        'uploader': 'import_users',
        'fileObjName' : 'import_users',
        'cancelImg': '/static/img/uploadify-cancel.png',
        'queueID': 'fileQueue',
        'auto': false,
        'multi': true,
        'method' : 'get', 
        'removeCompleted' : true,
         'fileTypeDesc' : 'Excel Files',
        'fileTypeExts' : '*.xls;*.xlsx',
        'onUploadStart' : function(file) {
        	var warehouse_id = $('div#warehouse_id').html();
        	warehouse_id = encodeURI(warehouse_id);
            $("#uploadify").uploadify("settings", "formData", {'warehouse_id' : warehouse_id});
        },
         'onUploadSuccess' :function(file,data,res){
         	data = JSON.parse(data);
         	if (data.err != '0') {
         		console.log(data.content);
         		kesn_prompt('fail',data.content);
         	}else{
         		$("#import_users").modal('hide');
         		kesn_prompt('success',data.content);
         		window.location.reload();
         	}
         }

    });

	
	$('#add_user').click(function(){
		var _html = '<tr class="_item"> <th scrope="row"></th><td class="td-ouser_name"><input type="text" class="form-control input-sm" placeholder="" name="ouser_name"/></td><td class="password" style="display: none;"></td><td><div class="created"></div></td><td class="change_button"><div><a href="#" class="btn btn-info sure_ouser btn-sm">确认<span class="fui-check-circle btn-tip"></span></a><a href="#" class="btn btn-default cancle_ouser btn-sm">取消<span class="fui-trash btn-tip"></span></a></div></td></tr>';
		$('tbody').append(_html);
	});
	
	$('tbody').on('click','.sure_ouser',function(){
		var _this = $(this);
		var ouser_name = _this.parents('._item').find('input[name=ouser_name]').val();
		ouser_name = $.trim(ouser_name);
		var warehouse_id = $('div#warehouse_id').html();
		if (ouser_name == '') {
			alert('用户名不能为空');
			return;
		}
		  $.ajax({
		      url: "add_ouser",    
		      dataType: "json",   
		      async: true, 
		      data: { "ouser_name": ouser_name,"warehouse_id":warehouse_id},    
		      type: "POST",   
		      success: function(data) {
		        if (data.err == '0') {
		        	var _data = data.data;
		        	var change_button_html = '<div><a href="#" class="btn btn-primary edit_ouser btn-sm" data-status="0">修改<span class="fui-new btn-tip"></span></a><a href="#" class="btn btn-danger kesn_del btn-sm">删除<span class="fui-cross btn-tip"></span></a></div></td>';
		        	var _item = _this.parents('._item');
		        	_item.find('.change_button').html(change_button_html);
		        	_item.find('th[scrope=row]').html(_data.ouser_id);
		        	_item.find('.kesn_del').attr('data-_id',_data.ouser_id);
		        	_item.find('.kesn_del').attr('data-_name',ouser_name);
		        	_item.find('div.created').html(_data.created);
		        	_item.find('td.td-ouser_name').html(ouser_name);
		        	kesn_prompt('success',data.content);
		        }else{
		        	kesn_prompt('fail',data.content);
		        }
		      },
		     
		});
	});

	//鍙栨秷
	$('tbody').on('click','.cancle_ouser',function(){
		var _this = $(this);
		_this.parents('._item').remove();

	});

	//鍒犻櫎
	$('tbody').on('click','.kesn_del',function(){
		var _this = $(this);
		var _id = _this.attr('data-_id');
		var _name = _this.attr('data-_name');
		var _url = 'del_ouser';
		var message = '您确定要删除 '+_id+': '+_name+' 吗？';
		kesn_del(_id,_url,message,_this);
	});

	//淇敼
	$('tbody').on('click','.edit_ouser',function(){
		var _this = $(this);
		var _this_item = _this.parents('._item');
		var _status = _this.attr('data-status');
		if (_status == '0') {
			$('.password_header').css('display','block');
			$('.password').css('display','block');
			_this_item.find('.password').html('<input class="form-control input-sm" type="text" name="password"></input>');
			var ouser_name = _this_item.find('.td-ouser_name').html();
			_this_item.find('.td-ouser_name').html('<input type="text" class="form-control input-sm" placeholder="" name="ouser_name" value="'+ouser_name+'"/>');
			var status1_html = '确认<span class="fui-check btn-tip"></span>';
			_this.html(status1_html);
			_this.attr('data-status','1');
		}else{
			var ouser_id = _this_item.find('th[scrope=row]').html();
			var ouser_name = _this_item.find('input[name=ouser_name]').val();
			var password = _this_item.find('input[name=password]').val();
			$.ajax({
			      url: "edit_ouser",  
			      dataType: "json",  
			      async: true, 
			      data: {"ouser_id":ouser_id, "ouser_name":ouser_name,"password":password},  
			      type: "POST",
			      success: function(data) {
			      	if (data.err == '0') {
			      		$('.password_header').css('display','none');
						$('.password').css('display','none');
						_this_item.find('.td-ouser_name').html(ouser_name);
						_this.html('编辑<span class="fui-new btn-tip"></span></a>');
						_this.attr('data-status','0');
						kesn_prompt('success',data.content);
			      	}else{
			      		kesn_prompt('fail',data.content);
			      	}
			      },
			     
			});

		}
	});
</script>

</footer>
</body>
</html>